<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="news.bll.topic.*"%>
<%@ page import="news.entity.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'do_addtopic.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
    	request.setCharacterEncoding("utf-8");
    	String tname = request.getParameter("tname");
    	TopicDaoImpl tdi = new TopicDaoImpl();
    	List<Topic> topics = tdi.getAllTopics();
    	List<String> tnames = new ArrayList<String>();
    	for(int i = 0; i<topics.size(); i++){
    		tnames.add(topics.get(i).getTname());
    	}
    	if(!tnames.contains(tname)){
    		int result = tdi.addTopic(tname);
        	if(result>0){
        		%>
        		<script>
        			alert("主题添加成功！");
        			window.location="admin.jsp";
        		</script>    		
        		<%
        	}else{
        		%>
        		<script>
        			alert("主题添加失败！");
        			window.location="topic_add.jsp";
        		</script>    		
        		<%
        	}
    	}else{
    		%>
    		<script>
    			alert("当前主题已存在，请重新输入不同的主题！");
    			window.location="topic_add.jsp";
    		</script>    		
    		<%
    	}
    	
    %>
  </body>
</html>
